package com.future.bantang.view;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.bantang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 16-1-18.
 */
public class LabelSetView extends LinearLayout {
    //保存所有的linearlayout
    private List<LinearLayout> layoutList;
    //新添加的linearlayout的布局参数
    private LayoutParams layoutParams;
    //新增的textview的布局参数
    private LayoutParams tvParams;
    //记录当前linearlayout的索引值
    private int curLayoutIndex=0;
    //记录屏幕密度
    private float density;
    //记录添加label后layout里剩余的宽度
    private int pixelLeft=-1;


    public LabelSetView(Context context) {
        this(context, null);
    }

    public LabelSetView(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.setOrientation(VERTICAL);
        layoutList=new ArrayList<>();

        WindowManager windowManager= (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outmetrics=new DisplayMetrics();
        windowManager.getDefaultDisplay().getMetrics(outmetrics);
        density = outmetrics.density;

        layoutParams=new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        tvParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public void addLabel(String labelStr){
        TextView tvLabel=getLabelTextView(labelStr);

        LinearLayout linearLayout=null;
        if (layoutList.size()<=curLayoutIndex||tvLabel.getMeasuredWidth()>pixelLeft){
            linearLayout=addLayout();
        }else {
            linearLayout=layoutList.get(curLayoutIndex);
        }

        pixelLeft=addLabelToLaxyout(linearLayout,tvLabel);

    }

    /**
     * 添加新的linearlayout
     * @return
     */
    private LinearLayout addLayout(){
        LinearLayout linearLayout=new LinearLayout(getContext());
        linearLayout.setLayoutParams(layoutParams);
        linearLayout.setOrientation(HORIZONTAL);
        this.addView(linearLayout);
        layoutList.add(linearLayout);
        return linearLayout;
    }

    /**
     * 获取新的label(textview)
     * @param labelStr
     * @return
     */
    private TextView getLabelTextView(String labelStr){
        TextView textView=new TextView(getContext());
        textView.setLayoutParams(tvParams);
        textView.setPadding((int)(5*density),(int)(5*density),(int)(5*density), (int) (20*density));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        textView.setTextColor(getResources().getColor(R.color.light_gray));
        textView.setText(labelStr);
        return textView;
    }

    private int addLabelToLaxyout(LinearLayout layout,TextView label){
        layout.addView(label);
        return pixelLeft+label.getMeasuredWidth();
    }


}
